package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.VideoApi;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * 小视频服务接口实现类
 */
@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    /**
     * 视频发布
     * @param video
     */
    @Override
    public void add(Video video) {
        video.setVid(idWorker.getNextId("video"));//推荐系统有关主键id
        video.setCreated(System.currentTimeMillis());//视频发布时间
        mongoTemplate.insert(video);
    }

    /**
     * 根据vids查询视频表
     * @param listVids
     * @return
     */
    @Override
    public List<Video> findByVids(List<Long> listVids) {
        Query query = new Query();
        query.addCriteria(Criteria.where("vid").in(listVids));
        return mongoTemplate.find(query,Video.class);
    }
    /**
     * 根据分页参数按照时间倒序查询视频表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Video> findPage(long page, Long pageSize) {
        //定义返回PageResult<Video>
        PageResult<Video> pageResult = new PageResult<>();
        Query query = new Query();
        //1查询总记录数
        long counts = mongoTemplate.count(query, Video.class);
        long start = (page - 1) * pageSize;
        //2查询当前页面需要展示的数据
        List<Video> videoList = new ArrayList<>();
        if (counts > start) { //20 > 27
            //构造分页limit 0,3
            query.limit(pageSize.intValue());//每页查询几条
            query.skip(start);//跳过1条数据
            //query.with(PageRequest.of(page-1,pageSize));
            query.with(Sort.by(Sort.Direction.DESC, "created"));
            videoList = mongoTemplate.find(query, Video.class);
        }
        return new PageResult<>(page, pageSize, counts, videoList);
    }


    /**
     * 小视频-点赞
     * @param id
     */
    @Override
    public Integer addLike(String id) {
        //1.对likecount进行加1 返回点赞总数
        Update updateOne = new Update();//更新的值
        updateOne.inc("likeCount", 1);//可以根据评论类型 更新不同的xx数量
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();//更新值之后返回最新的值
        findAndModifyOptions.returnNew(true);//返回最新的修改的值
        Query query = new Query(Criteria.where("_id").is(id));
        Video mm = mongoTemplate.findAndModify(query, updateOne, findAndModifyOptions, Video.class);

        Integer likeCount = mm.getLikeCount();

        return likeCount;
    }

    /**
     * 视频-点赞取消
     */
    @Override
    public Integer dislike(String id) {
        //1.对likecount进行-1 返回点赞总数
        Update updateOne = new Update();//更新的值
        updateOne.inc("likeCount", -1);//可以根据评论类型 更新不同的xx数量
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();//更新值之后返回最新的值
        findAndModifyOptions.returnNew(true);//返回最新的修改的值
        Query query = new Query(Criteria.where("_id").is(id));
        Video mm = mongoTemplate.findAndModify(query, updateOne, findAndModifyOptions, Video.class);

        Integer likeCount = mm.getLikeCount();

        return likeCount;
    }

    /**
     * 小视频-点赞
     * @param id
     */
    @Override
    public Integer Like(String id) {
        //1.对likecount进行加1 返回点赞总数
        Update updateOne = new Update();//更新的值
        updateOne.inc("likeCount", 1);//可以根据评论类型 更新不同的xx数量
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();//更新值之后返回最新的值
        findAndModifyOptions.returnNew(true);//返回最新的修改的值
        Query query = new Query(Criteria.where("_id").is(id));
        Comment mm = mongoTemplate.findAndModify(query, updateOne, findAndModifyOptions, Comment.class);

        Integer likeCount = mm.getLikeCount();

        return likeCount;
    }

    /**
     * 视频-点赞取消
     */
    @Override
    public Integer dislikes(String id) {
        //1.对likecount进行-1 返回点赞总数
        Update updateOne = new Update();//更新的值
        updateOne.inc("likeCount", -1);//可以根据评论类型 更新不同的xx数量
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();//更新值之后返回最新的值
        findAndModifyOptions.returnNew(true);//返回最新的修改的值
        Query query = new Query(Criteria.where("_id").is(id));
        Comment mm = mongoTemplate.findAndModify(query, updateOne, findAndModifyOptions, Comment.class);

        Integer likeCount = mm.getLikeCount();

        return likeCount;
    }


    /**
     * Video
     * @param id
     * @return
     */
    @Override
    public Video getVideo(String id) {
        Query query=new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query,Video.class);
    }




    /**
     * 小视频评论列表查询
     */
    @Override
    public PageResult<Comment> commentsList(Long page, Long pageSize, String id) {
        //定义返回PageResult<Comment>
        Query query = new Query( Criteria.where("publishId").is(new ObjectId(id)));

        //1查询总记录数
        long counts = mongoTemplate.count(query, Comment.class);
        long start = (page - 1) * pageSize;
        //2查询当前页面需要展示的数据
        List<Comment> commentList = new ArrayList<>();
        if (counts > start) { //20 > 27
            //构造分页limit 0,3
            query.limit(pageSize.intValue());//每页查询几条
            query.skip(start);//跳过1条数据
            //query.with(PageRequest.of(page-1,pageSize));
            query.with(Sort.by(Sort.Direction.DESC, "created"));
            commentList = mongoTemplate.find(query, Comment.class);
        }
        return new PageResult<>(page, pageSize, counts, commentList);
    }
}
